/*=================================================
    File:	zcom_uiIMDraft.c

	Module:	ui
	Author:	Mike	

=================================================*/

#include <zcom_uiIMMain.h>
#include <IMManager.h>
#include <ui.h>
#include <string.h>
#include <display.h>
#include <zcom_uiTextDialog.h>

#if 1
#define dbgf	debugf
#endif //1

extern char szMsg_Content[];
extern int  Len_szMsg_Content;
 
static void _zcom_uiIMDraftGetMsgCB(char *buf, int bufsize,
        LangIdx idx, int index, void *data)
{
    sprcpy(buf, bufsize, ""); 
	int id = (int) data;
	if(id<0)
		return;

	IM_DRAFTBOX  draft = {};
	if (IM_DraftBox_GetMsgByIndex(id, &draft))
	{
		sprcpy(buf, bufsize, draft.msg);
	}
}

static void _Draft_OptionSend(void * arg);

static void _EditSave(void * arg)
{
	int id = (int) arg;
	if(id<0 || strlen(szMsg_Content)==0)
		return;

	//BUGBUG
	//poor flash cannot support delete & add at the same time!!! 
#if 0
	//delete it first
	IM_DraftBox_DeleteMsgByIndex(id);

	//now save it
	if(IM_DraftBox_AddMsg(szMsg_Content)>=0)
#else
	//Do update directly! 
	if(IM_DraftBox_UpdateMsg(id, szMsg_Content)>=0)
#endif
	{
		uiMenuDialogMesgTimed(
					LANG_Menu_IM_INBOX_Message,
					langGet(LANG_Menu_IM_Save_Ok) 
					);
	}
	else
	{
		uiMenuDialogMesgTimed(
					LANG_Menu_IM_INBOX_Message,
					langGet(LANG_Menu_IM_Save_Fail) 
					);
	}

	//go back to draft
	uiMenuPop(1);
	uiMenuPop(1);
	uiMenuPop(1);
	uiMenuPop(1);
	uiMenuPop(1);

	//back again
	uiMenuPop(1);
	zcom_uiIMDraftStart(NULL);
}

static void _OptionEditDone(void *data, const char *str)
{
	if(str && strlen(str)>0)
	{
		//save
		memset(szMsg_Content, 0, Len_szMsg_Content);
		sprcpy(szMsg_Content, Len_szMsg_Content, str);
		
		//show menu list
		UIMenuListInit init;
		static UIMenuNode MenuOptionNodes[2] = {};
		MenuOptionNodes[0].text.textIdx = LANG_Menu_IM_Send; //send
		MenuOptionNodes[0].selectCB    = _Draft_OptionSend;
		MenuOptionNodes[0].selectCBarg = NULL;
		
		MenuOptionNodes[1].text.textIdx = LANG_Calllog_Select_save; //save
		MenuOptionNodes[1].selectCB    = _EditSave;
		MenuOptionNodes[1].selectCBarg = data;
				
		memset(&init, 0, sizeof(init));
		init.title.textIdx = LANG_SoftKey_Options;
		init.nodes          = MenuOptionNodes;
		init.nNodes         = 2;
		
		/*
		* Init menu to active
		*/
		uiMenuListInit(&init);
	}
}

static void _Draft_OptionEdit(void * data)
{
	int id = (int) data;
	if(id<0)
		return;

	IM_DRAFTBOX  draft = {};
	if (IM_DraftBox_GetMsgByIndex(id, &draft))
	{
		UIMenuPKeyTextEntryInit init = {};
		
		init.title.textIdx	 = LANG_Menu_IM_INBOX_Message;
		init.pkeyCB          = _OptionEditDone;
		init.pkeyCBarg		 = data;
				
		sprcpy(init.str, sizeof(init.str), draft.msg);
		init.cursorPos = 0;

		//set text for positive key
		init.pkeyTextNotNilEntryIdx = LANG_SoftKey_Options;
		init.pkeyTextNilEntryIdx = LANG_SoftKey_Options;
		
		uiMenuPKeyTextEntryInit(&init);
	}
}

static void _MsgSendDone(void * data)
{
	char * str = (char *)data;

	if(str && strlen(str) && strlen(szMsg_Content))
	{
		//get number
		char number[CFG_PHONEBOOK_DIGIT_MAX+1]={};
		sprcpy(number, CFG_PHONEBOOK_DIGIT_MAX, str);
		
		//pop
		uiMenuPop(1);

		//back again, according to KM
		uiMenuPop(1);

		//send now
		{
			dbgf("Send msg to %s\n", number);
			int err = IMManager_SendOutMsg(number, szMsg_Content);
			if(err>=0)
			{
				uiMenuDialogMesgTimed(
					LANG_Menu_IM_Send,
					langGet(LANG_Menu_IM_Send_Ok) 
					);
			}
			else
			{
				dbgf("Send error(code: %d)!\n", err);
				uiMenuDialogMesgTimed(
					LANG_Menu_IM_Send,
					langGet(LANG_Menu_IM_Send_Fail) 
					);
			}
		}
	}
}

static void _MsgSendSearchReady(void *data, const char *str)
{
	if(!str || strlen(str)==0)
	{
		//pop it...
		uiMenuPop(1);
		uiMenuPop(1);

		//warning
		uiMenuDialogMesgTimed(
            LANG_PhoneBook_title,
            langGet(LANG_ZCOM_Test_WLAN_Text_Not_Ready) 
            );
		return;
	}
	
	char number[CFG_PHONEBOOK_DIGIT_MAX+1] = {};
	sprcpy(number, sizeof(number), str);

	uiMenuPop(1);
	//just send it...
	_MsgSendDone((void *)number);
	return;
}

static void _MsgSendSearch(void * data)
{
	bool hasSIP = false;
	int mode = 0;
	for(int i=0;i<PhoneBook_GetCount();i++)
	{
		if(PhoneBook_RecordGetMode(i, &mode)==0)
		{
			if(!mode)
			{
				hasSIP = true;
				break;
			}
		}
	}
	
	if(!hasSIP)
	{
		//warning
		uiMenuDialogMesgTimed(
            LANG_PhoneBook_title,
            langGet(LANG_ZCOM_Test_WLAN_Text_Not_Ready) 
            );
		return;	
	}
	else
	{
		//use customized text-list menu here
		//by Mike
		UIMenuText_List_PBook_V2_EntryInit init;
		memset(&init, 0, sizeof(init));
		
		init.title.textIdx   = LANG_PhoneBook_search_name;
		
		init.prompt.textIdx  = LANG_PhoneBook_enter_search_name;
		init.doneCB          = _MsgSendSearchReady;
		init.pkeyTextEntryIdx = LANG_Menu_IM_Send;
		
		init.str[0]          = '\0';
		init.bufSize          = CFG_PHONEBOOK_DIGIT_MAX;
		init.cursorPos       = 0;
		init.shift           = 1;
		
		uiMenuText_List_PBook_V2_EntryInit(&init);
	}
}

static void _MsgSendPkeyClicked(void *arg, const char *str)
{

	if(str && strlen(str)>0)
	{
		//done
		_MsgSendDone((void *)str);
		
	}
	else
	{
		//do search
		_MsgSendSearch(NULL);		
	}

}

static void _Draft_OptionSend(void * arg)
{
	if(!IMManager_CanSend())
	{
		uiMenuDialogMesgTimed(
            LANG_Menu_IM_Send,
            langGet(LANG_ZCOM_Test_WLAN_Text_Not_Ready) 
            );
		return;
	}
	
	UIMenuPKeyTextEntryInit init;
    memset(&init, 0, sizeof(init));

    init.title.textIdx   = LANG_Menu_IM_Send_To;

    init.pkeyCB          = _MsgSendPkeyClicked;
	init.pkeyTextNotNilEntryIdx = LANG_Menu_IM_Send;
	init.pkeyTextNilEntryIdx = LANG_Menu_Call_Search_title;

    init.str[0]          = '\0';
    init.bufSize          = CFG_PHONEBOOK_DIGIT_MAX;
    init.cursorPos       = 0;
    init.nodeCBArgEx = arg;

    uiMenuPKeyTextEntryInit(&init);
	
	
}

static void _Draft_DeleteDone(void * data)
{
	int id = (int) data;
	if(id<0)
		return;

	//delete the message
	//by Mike
	IM_DraftBox_DeleteMsgByIndex(id);

	//return to the draft page...
	uiMenuPop(1);
	uiMenuPop(1);
	uiMenuPop(1);

	//back again
	uiMenuPop(1);
	zcom_uiIMDraftStart(NULL);

	uiMenuDialogMesgTimed(
            LANG_Menu_IM_INBOX_Message,
            langGet(LANG_PhoneBook_deletefinish) 
            );
}

static void _Draft_OptionDelete(void * data)
{
	uiMenuDialogMesgYes(
	    LANG_Menu_IM_INBOX_Message,
	    langGet(LANG_Menu_IM_Delete_Query),
	    _Draft_DeleteDone, data,
	    NULL, NULL, NULL);
}

static void scrollText_selectOption(void * data)
{
	UIMenuListInit init;
	static UIMenuNode MenuSelectNodes[3] = {};
    MenuSelectNodes[0].text.textIdx = LANG_Menu_Call_Search_edit; //edit
    MenuSelectNodes[0].selectCB    = _Draft_OptionEdit;
    MenuSelectNodes[0].selectCBarg = data;

    MenuSelectNodes[1].text.textIdx = LANG_Menu_IM_Send; //send
    MenuSelectNodes[1].selectCB    = _Draft_OptionSend;
    MenuSelectNodes[1].selectCBarg = NULL;

    MenuSelectNodes[2].text.textIdx = LANG_PhoneSetup_Profiles_delete; //delete
    MenuSelectNodes[2].selectCB    = _Draft_OptionDelete;
    MenuSelectNodes[2].selectCBarg = data;
	
	memset(&init, 0, sizeof(init));
	init.title.textIdx = LANG_SoftKey_Options;
	init.nodes          = MenuSelectNodes;
	init.nNodes         = 3;

	/*
	* Init menu to active
	*/
	uiMenuListInit(&init);
}

static void scrollText_selectBack(void * arg)
{
	uiMenuPop(1);
}


static void _zcom_uiIMDraftSelected(void * data)
{
	//add your codes here
	int id = (int) data;
	if(id<0)
		return;

	IM_DRAFTBOX  draft = {};
	if (IM_DraftBox_GetMsgByIndex(id, &draft))
	{
		//save msg to szMsg_Content
		//do not use local variant to pass-init
		memset(szMsg_Content, 0, Len_szMsg_Content);
		sprcpy(szMsg_Content, Len_szMsg_Content, draft.msg);
		
		UIMenuText title = {};
		SoftKeyParameter pos = {};
		SoftKeyParameter neg = {};

		title.textIdx = LANG_Menu_IM_INBOX_Message; //message

		pos.textIdx = LANG_SoftKey_Options; 
		pos.selectCB = scrollText_selectOption;
		pos.selectCBArg = data;

		neg.textIdx = LANG_SoftKey_Back; 
		neg.selectCB = scrollText_selectBack;
		
		//init
		zcom_uiTextDialogInit(szMsg_Content, strlen(szMsg_Content), &title, &pos, &neg);

	}
}

void zcom_uiIMDraftStart(void * arg)
{
	int used=0, total;
	if(IM_DraftBox_GetSize(&used, &total) && used>0)
	{
		UIMenuListInit init;
		static UIMenuNode menuSelectNodes[20];
		int nNodes = 0;
		memset(menuSelectNodes, 0, sizeof(menuSelectNodes));
		
		for (int index = used-1; index >= 0; index--)
		{
			menuSelectNodes[nNodes].text.textCB = _zcom_uiIMDraftGetMsgCB;
			menuSelectNodes[nNodes].text.textCBarg = (void *)index;
			menuSelectNodes[nNodes].selectCB    = _zcom_uiIMDraftSelected;
			menuSelectNodes[nNodes].selectCBarg = (void *)index;
			nNodes++;
			
			if(nNodes>=20)
				break;

		}
		
		memset(&init, 0, sizeof(init));
		init.title.textIdx  = LANG_Menu_IM_DRAFT;
		init.nodes          = menuSelectNodes;
		init.nNodes         = nNodes;
		
		/*
		* Init menu to active
		*/
		uiMenuListInit(&init);
		
	}
	else
	{
		char str[32];
		spr(str, sizeof(str), langGet(LANG_Menu_IM_Empty_Format), langGet(LANG_Menu_IM_DRAFT));
		uiMenuDialogMesgTimed(
				LANG_Menu_IM_DRAFT,
				str
				);
	}
}